﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Platform.BLL;
using Taurus.Parking.DTO;
using Taurus.Platform.DTO;
using Taurus.Platform.Net;
using HtmlAgilityPack;
using Taurus.Parking.XML;

namespace Taurus.Parking.BLL
{
    /// <summary>
    /// 车市
    /// </summary>
    class AutomobileMarket
    {
        //车市起始地址
        private const string AUTOMOBILE_MARKET_URL = "http://www.kaixin001.com/!parking/market.php?start={0}";

        private AccountBll _accountBll = new AccountBll();
        private CarPersistence _cp = new CarPersistence();
        private CarImage _ci = new CarImage();

        /// <summary>
        /// 同步xml文件
        /// </summary>
        private void SynchronizationCar(bool isCompulsory)
        {
            if (!isCompulsory && _cp.GetList(null).Count != 0)
            {
                //非强制更新，并且配置文件已经有数据则直接跳过
                return;
            }
            int pageSize = 8;
            UserAccount acc = _accountBll.GetRandomPublicAccount();
            HtmlDocument doc = Util.AccessPage(acc, string.Format(AUTOMOBILE_MARKET_URL, 0));
            int pageCount = Util.GetPageCount(doc, pageSize);
            List<Car> list = GetCarByHtmlDoc(doc);
            for (int i = 1; i < pageCount; i++)
            {
                list.AddRange(GetCarByHtmlDoc(Util.AccessPage(acc, string.Format(AUTOMOBILE_MARKET_URL, i * pageSize))));
            }

            //将list存储到XML，list为所有车型
            foreach (var item in list)
            {
                _cp.Update(item);
                _ci.UpateCarImage(item, isCompulsory);
            }
            _cp.Save();
        }

        private List<Car> GetCarByHtmlDoc(HtmlDocument doc)
        {
            List<Car> list = new List<Car>();
            foreach (var node in doc.DocumentNode.SelectNodes("//td"))
            {
                Car car = new Car();
                car.Name = node.SelectSingleNode(".//div[@class='tac'][1]").InnerText.Trim();
                //最后一个空格为空字符串
                if (car.Name != string.Empty)
                {
                    car.Price = Util.GetNumber(node.SelectSingleNode(".//div[@class='tac'][2]").InnerText).ToString();
                    
                    HtmlNode picNode = node.SelectSingleNode(".//div[@class='cs_pic']/img");
                    if (picNode == null)
                    {
                        //广告车型
                        picNode = node.SelectSingleNode(".//div[@class='cs_pic']/a/img");
                        car.PicFullPath = picNode.Attributes["src"].Value;
                        car.ID = Util.GetNumber(node.SelectSingleNode(".//div[@class='tac'][4]/a[1]").Attributes["href"].Value);
                    }
                    else
                    {
                        car.ID = Util.GetNumber(node.SelectSingleNode(".//div[@class='tac'][3]/a[1]").Attributes["href"].Value);
                        car.PicFullPath = picNode.Attributes["src"].Value;
                    }
                   
                    
                    list.Add(car);
                }
                
            }
            return list;
        }
        /// <summary>
        /// 强制更新车市资料
        /// </summary>
        public void UpdateAutombileMarketData()
        {
            SynchronizationCar(true);
        }
        /// <summary>
        /// 返回所有汽车
        /// </summary>
        /// <returns></returns>
        public List<Car> GetAllCar()
        {
            SynchronizationCar(false);
            return _cp.GetList(null);
        }
    }
}
